package oceanwiki.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * @FileName GlobalConfig
 * @Description
 * @Author Me
 * @date 2024-09-04
 **/

@Configuration
public class GlobalConfig {
    @Bean
    public CorsFilter corsFilter() {
        //创建cors对象
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        //支持域
        corsConfiguration.addAllowedOriginPattern("*");
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("*");
        corsConfiguration.setAllowCredentials(true);
        corsConfiguration.setMaxAge(3600L);//一个小时之内不需要重新验证


        //添加地址映射
        UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource= new UrlBasedCorsConfigurationSource();
        urlBasedCorsConfigurationSource.registerCorsConfiguration("/**", corsConfiguration);
        //返回CorsFilter对象

        return new CorsFilter(urlBasedCorsConfigurationSource);
    }
}
